package com.spring.sunflower.login;

import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.chuanglan.shanyan_sdk.OneKeyLoginManager;
import com.chuanglan.shanyan_sdk.listener.GetPhoneInfoListener;
import com.chuanglan.shanyan_sdk.listener.OneKeyLoginListener;
import com.chuanglan.shanyan_sdk.listener.OpenLoginAuthListener;
import com.chuanglan.shanyan_sdk.tool.ShanYanUIConfig;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.enums.PopupAnimation;
import com.spring.sunflower.MainActivity;
import com.spring.sunflower.R;
import com.spring.sunflower.base.BaseActivity;
import com.spring.sunflower.base.BaseView;
import com.spring.sunflower.constant.Constant;
import com.spring.sunflower.dialog.EnvironmentChoiceDialog;
import com.spring.sunflower.http.AppRetrofit;
import com.spring.sunflower.userinfo.GenderSelectActivity;
import com.spring.sunflower.util.AppUtils;
import com.spring.sunflower.util.StatusBarUtil;
import com.tencent.smtt.export.external.TbsCoreSettings;
import com.tencent.smtt.sdk.QbSdk;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;

/**
 * Created by Kevin on 2021/5/21<br/>
 * <p>
 * Describe:<br/>
 */
public class LoginActivity extends BaseActivity<LoginPresenter> implements BaseView, View.OnClickListener {

    private CheckBox checkBox;
    private TextView tvAgreement, tvPrivateAgreement;
    private TextView tvLoginByPhone, tvOneKeyLogin;
    private String userID;
    private LinearLayout llLoginByPhone;
    private ImageView ivIcon;
    private int clickTime = 0;
    private String currentEnvironment;
    private EnvironmentChoiceDialog dialog;

    @Override
    public void initView() {
        userID = getField(Constant.KEY_USERID, "");
        currentEnvironment = getField(Constant.KEY_ENVIRONMENT, Constant.ENVIRONMENT_PRODUCT);
        ivIcon = findViewById(R.id.ivIcon);
        tvLoginByPhone = findViewById(R.id.tvLoginByPhone);
        llLoginByPhone = findViewById(R.id.llLoginByPhone);
        tvOneKeyLogin = findViewById(R.id.tvOneKeyLogin);
        checkBox = findViewById(R.id.checkBox);
        tvAgreement = findViewById(R.id.tvAgreement);
        tvPrivateAgreement = findViewById(R.id.tvPrivateAgreement);
        StatusBarUtil.immersive(this);
        tvAgreement.setOnClickListener(this::onClick);
        tvPrivateAgreement.setOnClickListener(this::onClick);
        tvLoginByPhone.setOnClickListener(this);
        tvOneKeyLogin.setOnClickListener(this);
        llLoginByPhone.setOnClickListener(this);
        ivIcon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ++clickTime;
                if (clickTime == 5) {
                    showEnvironmentDialog();
                }
            }
        });

        AppUtils.initShanYanSDK(this);
        OneKeyLoginManager.getInstance().getPhoneInfo(new GetPhoneInfoListener() {
            @Override
            public void getPhoneInfoStatus(int code, String result) {
                if (code == 1022) {

                }
            }
        });
        OneKeyLoginManager.getInstance().setAuthThemeConfig(getShanYanUIConfig());
        if (currentEnvironment.equals(Constant.ENVIRONMENT_PRODUCT)) {//获取当前环境
            Constant.BASE_URL = Constant.BASE_URL_PRODUCT;
        } else {
            Constant.BASE_URL = Constant.BASE_URL_TEST;
        }
        saveField(Constant.KEY_ENVIRONMENT, currentEnvironment);
    }

    private void showEnvironmentDialog() {
        dialog = new EnvironmentChoiceDialog(LoginActivity.this, new EnvironmentChoiceDialog.OnCreateViewListener() {
            @Override
            public void onCreateView() {
                TextView tvOK = dialog.findViewById(R.id.tvOK);
                tvOK.setText("选择好了");
            }

            @Override
            public void onClickCancel() {
                clickTime = 0;
            }

            @Override
            public void onClickOK(String environment) {
               // showToast(environment);
                AppRetrofit.destroy();
                clickTime = 0;
                if (environment.equals(Constant.ENVIRONMENT_PRODUCT)) {
                    Constant.BASE_URL = Constant.BASE_URL_PRODUCT;
//                    showToast("生产");
                } else {
                    Constant.BASE_URL = Constant.BASE_URL_TEST;
//                    showToast("测试");
                }
                saveField(Constant.KEY_ENVIRONMENT, environment);
            }
        });
        new XPopup.Builder(LoginActivity.this)
                .popupAnimation(PopupAnimation.ScaleAlphaFromCenter)
                .isRequestFocus(false)
                .dismissOnTouchOutside(false)
                .asCustom(dialog)
                .show();
    }

    private ShanYanUIConfig getShanYanUIConfig() {
        ShanYanUIConfig.Builder builder = new ShanYanUIConfig.Builder();
        builder.setLogBtnImgPath(getResources().getDrawable(R.drawable.ic_btn_bg_click_enable))
                //自定义协议复选框选中时图片
                .setCheckedImgPath(getResources().getDrawable(R.drawable.ic_checkbox_checked))
                //设置自定义协议复选框未选中时图片
                .setUncheckedImgPath(getResources().getDrawable(R.drawable.ic_checkbox_unchecked))
                //设置协议复选框宽高
                .setCheckBoxWH(20, 20)
                .setcheckBoxOffsetXY(10, -1)
                .setCheckBoxMargin(0, 0, 0, 2)
//                .setStatusBarColor(getColorX(R.color.colorPrimary))
//                .setNavColor(getColorX(R.color.colorPrimary))
//                .setNavTextColor(getColorX(R.color.white))
                .setNavReturnImgPath(getResources().getDrawable(R.drawable.ic_back_material_dark))
                .setNavReturnBtnOffsetX(10)
                .setPrivacyNavColor(getColorX(R.color.colorPrimary))
                .setPrivacyStatusBarColor(getColorX(R.color.colorPrimary))
                .setPrivacyNavTextColor(getColorX(R.color.white))
                .setPrivacyNavReturnImgPath(getResources().getDrawable(R.drawable.ic_back_material_light))
                .setPrivacyNavReturnBtnOffsetX(10)
                .setAppPrivacyColor(getColorX(R.color.gray_4), getColorX(R.color.colorPrimary));

        return builder.build();
    }

    @Override
    public int getLayoutID() {
        return R.layout.activity_login;
    }

    @Override
    public LoginPresenter setPresenter() {
        return new LoginPresenter(this);
    }

    @Override
    public void requestFinishWithSth(String type) {
        super.requestFinishWithSth(type);
        if ("0".equals(type)) {
            startActivity(new Intent(this, GenderSelectActivity.class));
        } else {
            startActivity(new Intent(this, MainActivity.class));
//            startActivity(new Intent(this, GenderSelectActivity.class));
//            startActivity(new Intent(this, LocationActivity.class));
        }
        finish();
    }

    @Override
    public void onClick(View v) {
        if (isFastClick()) return;
        switch (v.getId()) {
            case R.id.llLoginByPhone:
            case R.id.tvLoginByPhone:
                //String field = getField(Constant.KEY_ENVIRONMENT, "");
               // showToast(field);
                if (checkBox.isChecked()) {
                    startNewActivity(LoginByPhoneActivity.class);
                } else {
                    showToast("请勾选同意协议");
                }
                break;
            case R.id.tvOneKeyLogin:
                if (checkBox.isChecked()) {
                    OneKeyLoginManager.getInstance().openLoginAuth(true, new OpenLoginAuthListener() {
                        @Override
                        public void getOpenLoginAuthStatus(int code, String result) {
                            printD("getOpenLoginAuthStatus" + "code=" + code + ",result=" + result);
                            if (1000 != code) {
                                showEnvironmentDialog(result);

                            }

                        }
                    }, new OneKeyLoginListener() {
                        @Override
                        public void getOneKeyLoginStatus(int code, String result) {
                            printD("getOneKeyLoginStatus" + "code=" + code + ",result=" + result);
                            if (1000 != code) {
                                showEnvironmentDialog(result);
                            } else {
                                try {
                                    JSONObject jsonObject = new JSONObject(result);
                                    if (jsonObject.has("token")) {
                                        String token = jsonObject.getString("token");
                                        mPresenter.loginByOneKey(userID, token);
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }

                        }
                    });
                } else {
                    showToast("请勾选同意协议");
                }
                break;
            case R.id.tvAgreement:
                goAgreement();
                break;
            case R.id.tvPrivateAgreement:
                goPrivateAgreement();
                break;
        }
    }

    private void showEnvironmentDialog(String result) {
        try {
            JSONObject jsonObject = new JSONObject(result);
            if (jsonObject.has("innerDesc")) {
                String innerDesc = jsonObject.getString("innerDesc");
                showToast(innerDesc);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
